import os, sys
from Sanae3D.lib.StructReader import StructReader
from Sanae3D.lib.ModelObject import Model3D

class Tantra_TMB(Model3D):
        
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(Tantra_TMB,self).__init__("XAC")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
    
    def parse_materials(self, numMat):
        
        for i in xrange(numMat):
            matCount = self.mat_count()
            texName = self.inFile.read_string(256, '\x00')
            self.create_material(matCount)
            self.add_material_name(matCount, "material[%d]" %matCount)
            self.add_texture_name(matCount, texName)
            print texName
        
    def parse_vertices(self, meshName, numVerts):
        
        for i in xrange(numVerts):
            vx, vy, vz = self.inFile.read_float(3)
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
            
        for i in xrange(numVerts):
            nx, ny, nz = self.inFile.read_float(3)
            self.add_vert_normals(meshName, i, [nx, ny, nz])
            
        for i in xrange(numVerts):
            tu, tv = self.inFile.read_float(2)
            self.add_vert_uv(meshName, i, [tu, tv])
    
    def parse_faces(self, meshName, numFaces, matNum):
        
        for i in xrange(numFaces):
            v1, v2, v3 = self.inFile.read_long(3)
            
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
            self.add_face_material(meshName, i, matNum)
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        version, revision, numMesh = self.inFile.read_long(3)
        for i in range(numMesh):
            meshName = self.inFile.read_string(256, "\x00")
            self.create_object(meshName)
            
            numVerts = self.inFile.read_long()
            self.parse_vertices(meshName, numVerts)
            
            numMatrix = self.inFile.read_long()
            self.inFile.read(4 * (numMatrix - 1))

            for j in xrange(numMatrix):
                self.inFile.read_float(16)
            
            matCount = self.mat_count()
            count, numFaces, unk1 = self.inFile.read_long(3)
            self.inFile.read(4 * (numMatrix - 1))
            self.parse_faces(meshName, numFaces, matCount)
            
            self.parse_materials(numMatrix)
            data = self.inFile.read(40)
            
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = Tantra_TMB(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "", "TMB", "Tantra Online TMB"

if __name__ == '__main__':
    
    file1 = "ITEM_Fallen_Bracelet_01.tmb"
    obj = read_file(file1)